﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestRenameInDomain
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWnd : Window
    {
        public MainWnd()
        {
            InitializeComponent();
        }

        private void m_RenameButton_Click(object sender, RoutedEventArgs e)
        {
            NativeCode utilityObject = new NativeCode();
            String username = m_SuperuserTxtBox.Text;
            String password = m_PasswordTxtBox.Text;
            String currentPcName = m_CurrentPcName.Text;
            String newPcName = m_NewPcName.Text;

            if (String.IsNullOrEmpty(username) ||
                String.IsNullOrEmpty(password) ||
                String.IsNullOrEmpty(currentPcName) ||
                String.IsNullOrEmpty(newPcName))
            {
                MessageBox.Show("Please fill in all fields before pressing rename button.");
                return;
            }

            String errorMsg = null;
            Boolean success = false;

            try
            {
                success = utilityObject.renameMachineByWin32(currentPcName, newPcName, username, password);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception thrown in Click method:");
            }

            errorMsg = utilityObject.ErrorMessage;

            if (!String.IsNullOrEmpty(errorMsg))
            {
                MessageBox.Show(errorMsg, "Error thrown by Win32 API.");
                return;
            }

            if (success)
            {
                MessageBox.Show("Renaming operation successful.");
            }

        }
    }
}
